﻿using Framework.Resource;
using Framework.UILogic;
using Game.Util;
using UnityEngine;

namespace Framework.Procedure
{
    // Download
    public class ProcedureDownloadResource : ProcedureBase
    {
        private bool downFinish = false;
        private int uiSerialId;
        private UIManager m_uiManager;
        private ResourceManager m_resourceManager;
        private DownloadUI m_ui;
        public override void Enter(IProcedureOwner procedureOwner)
        {
            base.Enter(procedureOwner);
            downFinish = false;
            m_uiManager = GameEntry.GetComponent<UIManager>();
            m_resourceManager=GameEntry.GetComponent<ResourceManager>();
            
            m_resourceManager.DownRemoteResource(new DownloadCallback()
            {
                OnDownloadErrorFunction =OnDownloadError,
                OnDownloadOverFunction = OnDownloadOver,
                OnDownloadProgressUpdateFunction = OnDownloadProgress,
                OnStartDownloadFileFunction = OnStartDownloadFile,
            });
            // m_ui = m_resourceManager.DownloadUI;
            // m_ui.Open(this);
            // uiSerialId= m_uiManager
            //     .OpenUIForm(AssetUtility.GetUIAssetPath("DownloadUI"), "Top", 0, false, this, OnUIOpen);
        }

        
        void OnUIOpen(IUIForm uiForm, bool result)
        {
            if (uiForm.SerialId == uiSerialId && result)
            {
                
                // Download();
            }
        }

        
        public void Download(DownloadUI ui)
        {
            m_ui = ui;
            
            m_resourceManager.DownRemoteResource(new DownloadCallback()
            {
                OnDownloadErrorFunction =OnDownloadError,
                OnDownloadOverFunction = OnDownloadOver,
                OnDownloadProgressUpdateFunction = OnDownloadProgress,
                OnStartDownloadFileFunction = OnStartDownloadFile,
            });
        }

        public override void Update(IProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.Update(procedureOwner, elapseSeconds, realElapseSeconds);
            if (downFinish)
            {
                procedureOwner.ChangeState<ProcedureConfigInit>();
            }
        }

        public override void OnLeave(IProcedureOwner procedureOwner)
        {
            base.OnLeave(procedureOwner);
            m_uiManager.CloseUIForm(uiSerialId,null);
            // m_ui.Close();
        }

        void OnDownloadOver(bool isSucceed)
        {
            
            if (!isSucceed)
            {
                Debug.Log("下载失败");
            }
            else
            {
                downFinish = isSucceed;
            }
        }

        void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes,
            long currentDownloadBytes)
        {
            // m_ui.OnDownloadProgress(totalDownloadCount,currentDownloadCount,totalDownloadBytes,currentDownloadBytes);
        }

        void OnDownloadError(string fileName, string error)
        {
            // m_ui.OnDownloadError(fileName,error);
        }

        void OnStartDownloadFile(string fileName, long sizeBytes)
        {
            // m_ui.OnStartDownloadFile(fileName, sizeBytes);
        }
    }

}